
/**
 * @file bitalloc_core.c
 * A/52 core bit allocation
 */

#include "common.h"

#include <string.h>

#include "a52.h"
#include "a52tab.h"
#include "bitalloc_core.h"

static inline int calc_lowcomp1(int a, int b0, int b1, int c)
{
    if ((b0 + 256) == b1) {
        a = c;
    } else if (b0 > b1) {
        a = MAX(a - 64, 0);
    }
    return a;
}

static inline int calc_lowcomp(int a, int b0, int b1, int bin)
{
    if (bin < 7) {
        return calc_lowcomp1(a, b0, b1, 384);
    } else if (bin < 20) {
        return calc_lowcomp1(a, b0, b1, 320);
    } else {
        return MAX(a - 128, 0);
    }
}

void a52_bit_alloc_calc_psd(uint8_t *exp, int start, int end, int16_t *psd,
                            int16_t *band_psd)
{
    int bin, i, j, k, end1, v;

    /* exponent mapping to PSD */
    for (bin = start; bin < end; bin++)
        psd[bin] = a52_psd_tab[exp[bin]];

    /* PSD integration */
    j = start;
    k = a52_bin_to_band_tab[start];
    do {
        v = psd[j];
        j++;
        end1 = MIN(a52_band_start_tab[k+1], end);
        for (i = j; i < end1; i++) { // logadd
            int adr = MIN(ABS(v - psd[j]) >> 1, 255);
            v = MAX(v, psd[j]) + a52_log_add_tab[adr];
            j++;
        }
        band_psd[k]=v;
        k++;
    } while (end > a52_band_start_tab[k]);
}

void a52_bit_alloc_calc_mask(A52BitAllocParams *s, int16_t *band_psd, int start,
                             int end, int fast_gain, int16_t *mask)
{
    int16_t excite[50]; /* excitation */
    int bin;
    int bndstrt, bndend, begin, end1, tmp;
    int lowcomp, fastleak, slowleak;

    /* excitation function */
    bndstrt = a52_bin_to_band_tab[start];
    bndend  = a52_bin_to_band_tab[end-1] + 1;

    if (bndstrt == 0) {
        lowcomp = 0;
        lowcomp = calc_lowcomp1(lowcomp, band_psd[0], band_psd[1], 384);
        excite[0] = band_psd[0] - fast_gain - lowcomp;
        lowcomp = calc_lowcomp1(lowcomp, band_psd[1], band_psd[2], 384);
        excite[1] = band_psd[1] - fast_gain - lowcomp;
        begin = 7;
        for (bin = 2; bin < 7; bin++) {
            if (bin+1 < bndend)
                lowcomp = calc_lowcomp1(lowcomp, band_psd[bin], band_psd[bin+1], 384);
            fastleak = band_psd[bin] - fast_gain;
            slowleak = band_psd[bin] - s->sgain;
            excite[bin] = fastleak - lowcomp;
            if (bin+1 < bndend) {
                if (band_psd[bin] <= band_psd[bin+1]) {
                    begin = bin + 1;
                    break;
                }
            }
        }

        end1 = MIN(bndend, 22);

        for (bin = begin; bin < end1; bin++) {
            if (bin+1 < bndend)
                lowcomp = calc_lowcomp(lowcomp, band_psd[bin], band_psd[bin+1], bin);

            fastleak = MAX(fastleak - s->fdecay, band_psd[bin] - fast_gain);
            slowleak = MAX(slowleak - s->sdecay, band_psd[bin] - s->sgain);
            excite[bin] = MAX(fastleak - lowcomp, slowleak);
        }
        begin = 22;
    } else {
        /* coupling channel */
        begin = bndstrt;
        fastleak = (s->cplfleak << 8) + 768;
        slowleak = (s->cplsleak << 8) + 768;
    }

    for (bin = begin; bin < bndend; bin++) {
        fastleak = MAX(fastleak - s->fdecay, band_psd[bin] - fast_gain);
        slowleak = MAX(slowleak - s->sdecay, band_psd[bin] - s->sgain);
        excite[bin] = MAX(fastleak, slowleak);
    }

    /* compute masking curve */

    for (bin = bndstrt; bin < bndend; bin++) {
        tmp = s->dbknee - band_psd[bin];
        if (tmp > 0) {
            excite[bin] += tmp >> 2;
        }
        mask[bin] = MAX(a52_hearing_threshold_tab[bin >> s->halfratecod][s->fscod], excite[bin]);
    }

    /* TODO: delta bit allocation */
}

void a52_bit_alloc_calc_bap(int16_t *mask, int16_t *psd, int start, int end,
                            int snr_offset, int floor, uint8_t *bap)
{
    int i, j;

    // special case, if snr offset is -960, set all bap's to zero
    if (snr_offset == -960) {
        memset(bap, 0, 256);
        return;
    }

    i = start;
    j = a52_bin_to_band_tab[start];
    do {
        int v = (MAX(mask[j] - snr_offset - floor, 0) & 0x1FE0) + floor;
        int endj = MIN(a52_band_start_tab[j] + a52_critical_band_size_tab[j], end);
        while (i < endj) {
            int address = CLIP((psd[i] - v) >> 5, 0, 63);
            bap[i++] = a52_bap_tab[address];
        }
    } while (end > a52_band_start_tab[j++]);
}
